using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace ExampleParser
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("usage: ExampleParser <start directory>");
                return;
            }

            DirectoryInfo dir = null;
            try
            {
                dir = new DirectoryInfo(args[0]);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to read " + args[0] + ", error\n" + e.Message);
                return;
            }
            ProcessDirectory(dir);
        }

        static void ProcessDirectory(DirectoryInfo dir)
        {
            try
            {
                if (!dir.Exists)
                {
                    Console.WriteLine(dir.FullName + " does not exist, skipping.");
                    return;
                }
                if (dir.Name.Equals("Properties") || dir.Name.Equals("bin") || dir.Name.Equals("obj"))
                {
                    return;
                }

                FileInfo[] currentDirectory = dir.GetFiles();

                foreach (FileInfo file in currentDirectory)
                {
                    if (file.Extension.Equals(".cs"))
                    {
                        ProcessFile(file);
                    }
                }
                DirectoryInfo[] sub = dir.GetDirectories();
                foreach (DirectoryInfo next in sub)
                {
                    ProcessDirectory(next);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("error accessing " + dir.FullName + ", skipping :\n" + e.Message);
                return;
            }
        }

        static void ProcessFile(FileInfo file)
        {
            if (!file.Exists)
            {
                return;
            }

            string xmlName = Path.ChangeExtension(file.FullName, ".xml");
            string xmlShortName = Path.ChangeExtension(file.Name, ".xml");
            string htmlName = Path.ChangeExtension(file.FullName, ".html");
            string htmlShortName = Path.ChangeExtension(file.Name, ".html");

            using (StreamReader sr = file.OpenText())
            using (StreamWriter xmlWriter = new StreamWriter(xmlName))
            using (StreamWriter htmlWriter = new StreamWriter(htmlName))
            {
                Console.WriteLine("Processing " + file.Name + ", creating " + xmlShortName + " and " + htmlShortName);
                xmlWriter.WriteLine("<example><code>");
                htmlWriter.WriteLine("<pre>");

                string line = sr.ReadLine();
                while (line != null)
                {
                    line = line.Replace("&", "&amp;");
                    
                    line = line.Replace("\"", "&quot;");
                    line = line.Replace("<", "&lt;");
                    line = line.Replace(">", "&gt;");
                    htmlWriter.WriteLine(line);
                    line = line.Replace("'", "&apos;");
                    xmlWriter.WriteLine(line);
                    line = sr.ReadLine();
                }
                xmlWriter.WriteLine("</code></example>");
                htmlWriter.WriteLine("</pre>");
            }
        }
    }
}
